package com.zhiche.lisa.bms.controller.price;


import com.baomidou.mybatisplus.plugins.Page;
import com.zhiche.lisa.bms.dao.model.price.PriceDeductRate;
import com.zhiche.lisa.bms.pojo.vo.price.PriceDedRateVO;
import com.zhiche.lisa.bms.service.price.PriceDeductRateService;
import com.zhiche.lisa.core.enums.CommonEnum;
import com.zhiche.lisa.core.supports.BaseException;
import com.zhiche.lisa.core.supports.RestfulResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 支付扣点设置 前端控制器
 * </p>
 *
 * @author zhangkun
 * @since 2018-08-31
 */
@RestController
@RequestMapping("/priceDeductRate")
@Api(value = "priceDeductRate", description = "支付扣点维护", tags = "支付扣点维护")
public class PriceDeductRateController {
	public Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private PriceDeductRateService priceDeductRateService;

	@PostMapping(value = "/queryPriceDeductRatePage")
	@ApiOperation(value = "queryPriceDeductRatePageApi", notes = "支付扣点查询分页返回并按承运商名称排序", response = RestfulResponse.class)
	public RestfulResponse<Page<PriceDeductRate>> queryPriceDeductRatePage(@RequestBody Page page) {
		logger.info("controller:/PriceDeductRatePage/queryPriceDeductRatePage data: {}", page);
		RestfulResponse result = new RestfulResponse(0, "成功", null);

		try {
			Page priceDeductRate = priceDeductRateService.queryPriceDeductRate(page);
			result.setData(priceDeductRate);
		} catch (Exception e) {
			result.setCode(-1);
			result.setMessage(e.getMessage());

		}
		return result;
	}

	@GetMapping(value = "/returnPriceDeductRate/{fleetId}")
	@ApiOperation(value = "returnPriceDeductRateApi", notes = "根据车队获取扣点", response = RestfulResponse.class)
	public RestfulResponse<PriceDeductRate> returnPriceDeductRate(@PathVariable("fleetId") Long fleetId) {
		logger.info("controller:/PriceDeductRatePage/queryPriceDeductRatePage data: {}", fleetId);
		RestfulResponse<PriceDeductRate> result = new RestfulResponse(0, "成功", null);
		try {
			PriceDeductRate priceDeductRate = priceDeductRateService.returnPriceDeductRate(0L, fleetId);
			result.setData(priceDeductRate);
		} catch (Exception e) {
			result.setCode(-1);
			result.setMessage(e.getMessage());

		}
		return result;
	}

	@PostMapping("/insertPriceDeductRate")
	@ApiOperation(value = "insertPriceDeductRateApi", notes = "新增一条数据并更新上一条的有效期", response = RestfulResponse.class)
	@ApiResponses({@ApiResponse(code = 0, message = "success"), @ApiResponse(code = -1, message = "系统异常")})
	public RestfulResponse<Page<PriceDedRateVO>> insertPriceDeductRate(@RequestBody PriceDeductRate priceDeductRate) {
		logger.info("controller:/PriceDeductRatePage/queryPriceDeductRatePage data: {}", priceDeductRate);
		RestfulResponse<Page<PriceDedRateVO>> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(),
				CommonEnum.SUCCESS.getText(), null);
		try {
			priceDeductRateService.insertPriceDeductRate(priceDeductRate);
		} catch (BaseException e) {
			logger.error("FleetController.createFleet ERROR: {}", e);
			result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("FleetController.createFleet ERROR: {}", e);
			result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
		}
		return result;
	}
}

